package wstrader.task.trade.reports;

import pc.sql.value.BooleanValue;
import pc.sql.value.DateValue;
import pc.sql.value.IntValue;
import pc.sql.value.StringValue;
import pc.system.*;
import wstrader.task.GenReport;
import wstrader.task.trade.TradeTask;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import pc.util.*;

public class PriceFixed extends TradeTask
{
  public PriceFixed(Tasks owner)
  {
    super(owner,"price-fixed");
  }
  
  public long doTask()
  {
  	long Ret =0;
    int task_id = this.getRequest().getIntValue("task_id");
    switch (task_id)
	{
    	case 5: //Report
    		System.out.println("CName:-" + getUser().getComanyName().toUpperCase());
    		if ((getUser().getComanyName().toUpperCase()).compareTo("MERCAFE VIETNAM")==0)
    		{
    			getResponse().setReport(new Print_MercafePriceFixReport(this).generate());
    		}
    		else
    		{
    			getResponse().setReport(new PrintPriceFixReport(this).generate());
    		}
    		break;  
    		
        	default : //View only
            break;	
	}
  	
  	return Ret;
  }
  
  public ResultSet fetchUnfixedContract()
  {
  	int BuyerSellerId = getRequest().getIntValue("buyer_seller");
  	String TerminalMarket = getRequest().getStringValue("terminal_market");
  	String TerminalMonth = getRequest().getStringValue("terminal_month");
  	String Type = getRequest().getStringValue("cnt_type");
  	
  	if (TerminalMarket.compareTo("")==0) TerminalMarket ="0";
  	if (TerminalMonth.compareTo("")==0) TerminalMonth = "0";
  	
  	if (Type.compareTo("")==0) Type ="0";
  	
  	if (TerminalMonth.compareTo("0")==0)
  	{
  		String Sql ="Select * from contracts where " +
  				"(supplier_buyer_id = ? OR ?) " +			
				"and (terminal_market = ? OR ?) " +
				"and (type = ? OR ?) " +
				"and (contract_price_type = ?) order by terminal_market,terminal_month,type";
  	
  		Vector vp = new Vector();

  		vp.addElement(new IntValue(BuyerSellerId));
  		vp.addElement(new BooleanValue(BuyerSellerId == 0));

  		vp.addElement(new StringValue(TerminalMarket));
  		vp.addElement(new BooleanValue(TerminalMarket.compareTo("0") == 0));
  	
  		vp.addElement(new StringValue(Type));
  		vp.addElement(new BooleanValue(Type.compareTo("0") == 0));
    
  		vp.addElement(new StringValue("U"));
    
  		return getSQLCommand().executeQuery(Sql,vp);
  	}
  	else
  	{
  	  	java.sql.Date TerMth = Utils.strToDate(getRequest().getStringValue("terminal_month"));
  		String Sql ="Select * from contracts where " +
		"(supplier_buyer_id = ? OR ?) " +			
		"and (terminal_market = ? OR ?) " +
		"and (terminal_month = ? OR ?) " +
		"and (type = ? OR ?) " +
		"and (contract_price_type = ?) order by terminal_market,terminal_month,type";

  		Vector vp = new Vector();

  		vp.addElement(new IntValue(BuyerSellerId));
  		vp.addElement(new BooleanValue(BuyerSellerId == 0));

  		vp.addElement(new StringValue(TerminalMarket));
  		vp.addElement(new BooleanValue(TerminalMarket.compareTo("0") == 0));

  		vp.addElement(new DateValue(TerMth));
  		vp.addElement(new BooleanValue(TerMth.compareTo(new java.sql.Date(0)) == 0));

  		vp.addElement(new StringValue(Type));
  		vp.addElement(new BooleanValue(Type.compareTo("0") == 0));

  		vp.addElement(new StringValue("U"));

  		return getSQLCommand().executeQuery(Sql,vp);		
  	}
  	//ResultSet Rs = null;
  	
  	//Rs = getSQLCommand().executeQuery("select * from contracts where contract_price_type = 'U' and unfixed_tons > 0 order by terminal_market,terminal_month,type");
  	
  	//return Rs;
  }
 
}

class Print_MercafePriceFixReport extends GenReport
{
	private PriceFixed pricefixed = null;
	  public Print_MercafePriceFixReport(Tasks owner)
	  {
	    super(owner,"Mercafe_PriceFixed.xls");
	    this.pricefixed = (PriceFixed)owner;
	  }
	  
	  public String generate()
	  {
		writePurchase();
	  	return export();
	  }
	  
	  private void writePurchase()
	  {	  	
	  	ResultSet Rs=null;
	  	Rs = pricefixed.fetchUnfixedContract();
	  	
	  	int Loop=6;
	  	this.setSheet(0);
	  	try {
	  		
		  	HSSFRow TR = this.getRow((int)0);
		  	HSSFCell TC = TR.getCell((short)0);	  		
		  	HSSFRow R = this.getRow((int)Loop);
		  	HSSFCell C = R.getCell((short)0);
		  	
		  	String StrChkTerMth = "";
		  	String StrRecTerMth = "";
		  	int StFormulaRow = 0;
		  	
	  		Rs.beforeFirst();
			while (Rs.next())
			{
				StrRecTerMth = Utils.dateToStr(Rs.getDate("terminal_month")).substring(3);
				
				if (StrChkTerMth.compareTo(StrRecTerMth)!=0)
				{
					if (Loop>6)
					{
					this.setCellValue(5,Loop,StrChkTerMth + " Total");
					R=this.getRow((int)Loop);
					TC = TR.getCell((short)1);
					C = R.getCell((short)5);
					C.setCellStyle(TC.getCellStyle()); 					

					this.set_CellFormula(13,Loop,"SUM(N"+ StFormulaRow + ":N" +Loop+ ")");
					R=this.getRow((int)Loop);
					TC = TR.getCell((short)3);
					C = R.getCell((short)13);
					C.setCellStyle(TC.getCellStyle());
					
					this.set_CellFormula(14,Loop,"SUM(O"+ StFormulaRow + ":O" +Loop+ ")");
					R=this.getRow((int)Loop);
					TC = TR.getCell((short)3);
					C = R.getCell((short)14);
					C.setCellStyle(TC.getCellStyle()); 					
					}
					
					Loop=Loop+2;
					this.setCellValue(5,Loop,StrRecTerMth);
					R=this.getRow((int)Loop);
					TC = TR.getCell((short)1);
					C = R.getCell((short)5);
					C.setCellStyle(TC.getCellStyle()); 
					Loop=Loop+1;
					StrChkTerMth = StrRecTerMth;
				}
				
				StFormulaRow=Loop+1;
    			this.setCellValue(6,Loop,Rs.getString("contract_no"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)6);
				C.setCellStyle(TC.getCellStyle());    			
				String Ref ="";
				
				if (Rs.getString("supplier_buyer_ref")==null)
				{
					Ref = "-";
				}
				else
				{
					Ref = Rs.getString("supplier_buyer_ref");
				}	
				this.setCellValue(7,Loop,Ref);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)7);
				C.setCellStyle(TC.getCellStyle());
				
				this.setCellValue(8,Loop,pricefixed.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id")));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)8);
				C.setCellStyle(TC.getCellStyle());
				
				this.setCellValue(9,Loop,pricefixed.getFieldValueForId("grade_master","name",Rs.getInt("grade_id")));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)9);
				C.setCellStyle(TC.getCellStyle());
				
				this.setCellValue(10,Loop,Rs.getFloat("contract_price_foreign"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)10);
				C.setCellStyle(TC.getCellStyle());
				
				this.setCellValue(11,Loop,Rs.getFloat("differential"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)11);
				C.setCellStyle(TC.getCellStyle());
				
				String BotSold="-";
				if (Rs.getString("type").compareTo("P")==0)
				{
					BotSold="Sell";
				}
				else
				{
					BotSold="Buy";
				}
				this.setCellValue(12,Loop,BotSold);	
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)6);
				C = R.getCell((short)12);
				C.setCellStyle(TC.getCellStyle());
				
				this.setCellValue(13,Loop,Rs.getFloat("fixed_tons"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)13);
				C.setCellStyle(TC.getCellStyle());
				
				this.setCellValue(14,Loop,Rs.getFloat("unfixed_tons"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)14);
				C.setCellStyle(TC.getCellStyle());
				
				Loop=Loop+1;
			}
			this.setCellValue(5,Loop,StrChkTerMth + " Total");
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)1);
			C = R.getCell((short)5);
			C.setCellStyle(TC.getCellStyle()); 					

			this.set_CellFormula(13,Loop,"SUM(N"+ StFormulaRow + ":N" +Loop+ ")");
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)3);
			C = R.getCell((short)13);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(14,Loop,"SUM(O"+ StFormulaRow + ":O" +Loop+ ")");
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)3);
			C = R.getCell((short)14);
			C.setCellStyle(TC.getCellStyle()); 					
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }	  	  
}



class PrintPriceFixReport extends GenReport
{
	  private PriceFixed pricefixed = null;
	  public PrintPriceFixReport(Tasks owner)
	  {
	    super(owner,"PriceFixed.xls");
	    this.pricefixed = (PriceFixed)owner;
	  }
	  
	  public String generate()
	  {
		writeReport();
	  	return export();
	  }
	  
	  private void writeReport()
	  {
	  	String Type = pricefixed.getRequest().getStringValue("type");	  	
	  	ResultSet Rs=null;
	  	Rs = pricefixed.fetchUnfixedContract();
	  	int StSum=0;
	  	int EnSum=0;
	  	int Loop=8;
	  	float Fton=0;
	  	float UFton=0;
	  	String TerTot_F ="Sum(M";
	  	String TerTot_U ="Sum(N";
	  	try {
	  		
		  	Rs.beforeFirst();
		  	String TerMth="";
		  	String TerMrk="";
		  	String CType="";
		  	String CntType ="";
		  	String ChgTMth ="N";
		  	String OldTerMrk = "";
		  	
		  	HSSFRow TR = this.getRow((int)0);
		  	HSSFCell TC = TR.getCell((short)0);
		  	HSSFRow R = this.getRow((int)Loop);
		  	HSSFCell C = R.getCell((short)0);
		  		
			while (Rs.next())
			{
				if (TerMrk.compareTo(Rs.getString("terminal_market"))!=0)
				{
//--------------					
					if (TerMrk.compareTo("")!= 0)
					{
						this.setCellValue(4,Loop,"Total");
						R=this.getRow((int)Loop);
						TC = TR.getCell((short)0);
						C = R.getCell((short)4);
						C.setCellStyle(TC.getCellStyle());
						EnSum = Loop;
			  		
						this.set_CellFormula(12,Loop,"SUM(M" + StSum + ":M" + EnSum+ ")");
						TC = TR.getCell((short)3);
						R = this.getRow((int)Loop);
						C = R.getCell((short)12);
						C.setCellStyle(TC.getCellStyle());
					
						this.set_CellFormula(13,Loop,"SUM(N" + StSum + ":N" + EnSum+ ")");
						TC = TR.getCell((short)3);
						R = this.getRow((int)Loop);
						C = R.getCell((short)13);					
						C.setCellStyle(TC.getCellStyle());
						
						
						if (TerTot_F.compareTo("Sum(M")==0)
						{
							TerTot_F = TerTot_F + (Loop+1) + ")";
							TerTot_U = TerTot_U + (Loop+1) + ")";
						}
						else
						{
							TerTot_F = TerTot_F + ",M" + (Loop+1) + ")";
							TerTot_U = TerTot_U + ",N" + (Loop+1) + ")";							
						}
							
						Loop=Loop+2;
						this.setCellValue(3,Loop,TerMth +" Total");
						R=this.getRow((int)Loop);
						TC = TR.getCell((short)0);
						C = R.getCell((short)3);
						C.setCellStyle(TC.getCellStyle());
						
						this.set_CellFormula(12,Loop,TerTot_F);
						R=this.getRow((int)Loop);
						TC = TR.getCell((short)3);
						C = R.getCell((short)12);
						C.setCellStyle(TC.getCellStyle());
						
						this.set_CellFormula(13,Loop,TerTot_U);
						R=this.getRow((int)Loop);
						TC = TR.getCell((short)3);
						C = R.getCell((short)13);
						C.setCellStyle(TC.getCellStyle());						
						Loop=Loop+2;
				  		TerTot_F = "Sum(M";	
				  		TerTot_U = "Sum(N";
					}	
//--------------		
					OldTerMrk = TerMrk;
					this.setCellValue(2,Loop,"Contracts Against " + Rs.getString("terminal_market")+":");
					R=this.getRow((int)Loop);
					TC = TR.getCell((short)0);
					C = R.getCell((short)2);
					C.setCellStyle(TC.getCellStyle());
					TerMrk = Rs.getString("terminal_market");
					Loop=Loop+1;
				}
				
				if ((Utils.dateToStr(Rs.getDate("terminal_month")).substring(3)).compareTo(TerMth)!=0)
				{
					
					if (OldTerMrk.compareTo("LIFFE")!=0)
					{
						if (TerMth.compareTo("")!= 0)
						{
							this.setCellValue(4,Loop,"Total");
							R=this.getRow((int)Loop);
							TC = TR.getCell((short)0);
							C = R.getCell((short)4);
							C.setCellStyle(TC.getCellStyle());
							EnSum = Loop;
			  		
							this.set_CellFormula(12,Loop,"SUM(M" + StSum + ":M" + EnSum+ ")");
							TC = TR.getCell((short)3);
							R = this.getRow((int)Loop);
							C = R.getCell((short)12);
							C.setCellStyle(TC.getCellStyle());
					
							this.set_CellFormula(13,Loop,"SUM(N" + StSum + ":N" + EnSum+ ")");
							TC = TR.getCell((short)3);
							R = this.getRow((int)Loop);
							C = R.getCell((short)13);					
							C.setCellStyle(TC.getCellStyle());
						
						
							if (TerTot_F.compareTo("Sum(M")==0)
							{
								TerTot_F = TerTot_F + (Loop+1) + ")";
								TerTot_U = TerTot_U + (Loop+1) + ")";
							}
							else
							{
								TerTot_F = TerTot_F + ",M" + (Loop+1) + ")";
								TerTot_U = TerTot_U + ",N" + (Loop+1) + ")";							
							}
							
							Loop=Loop+2;
							this.setCellValue(3,Loop,TerMth +" Total");
							R=this.getRow((int)Loop);
							TC = TR.getCell((short)0);
							C = R.getCell((short)3);
							C.setCellStyle(TC.getCellStyle());
						
							this.set_CellFormula(12,Loop,TerTot_F);
							R=this.getRow((int)Loop);
							TC = TR.getCell((short)3);
							C = R.getCell((short)12);
							C.setCellStyle(TC.getCellStyle());
						
							this.set_CellFormula(13,Loop,TerTot_U);
							R=this.getRow((int)Loop);
							TC = TR.getCell((short)3);
							C = R.getCell((short)13);
							C.setCellStyle(TC.getCellStyle());						
							Loop=Loop+2;
				  			TerTot_F = "Sum(M";	
				  			TerTot_U = "Sum(N";	
						
				  		
				  			TerMth = Utils.dateToStr(Rs.getDate("terminal_month")).substring(3);
							ChgTMth = "Y";
						}
						else
						{
							TerMth = Utils.dateToStr(Rs.getDate("terminal_month")).substring(3);
						}
					}
					else
					{
						OldTerMrk = "NYC";
					}
					TerMth = Utils.dateToStr(Rs.getDate("terminal_month")).substring(3);
					this.setCellValue(3,Loop,TerMth);
				  	TC = TR.getCell((short)2);
				  	R = this.getRow((int)Loop);
				  	C = R.getCell((short)3);
				  	C.setCellStyle(TC.getCellStyle());
				  	Loop=Loop+1;
				  	

			  		
				  	if (CType.compareTo(Rs.getString("type"))==0)
				  	{
						CType = Rs.getString("type");
						if (CType.compareTo("P")==0) CntType = "Purchase Contracts";
						if (CType.compareTo("S")==0) CntType = "Sales Contracts";
						this.setCellValue(4,Loop,CntType);
						R=this.getRow((int)Loop);
						TC = TR.getCell((short)1);
						C = R.getCell((short)4);
						C.setCellStyle(TC.getCellStyle());
						Loop=Loop+1;
				  	}
				}
				
				if (CType.compareTo(Rs.getString("Type"))!=0)
				{
					if (ChgTMth.compareTo("N")== 0)
					{
						if (Loop > 10)
						{
						this.setCellValue(4,Loop,"Total");
						R=this.getRow((int)Loop);
						TC = TR.getCell((short)0);
						C = R.getCell((short)4);
						C.setCellStyle(TC.getCellStyle());
						EnSum = Loop;
			  		
						this.set_CellFormula(12,Loop,"SUM(M" + StSum + ":M" + EnSum+ ")");
						TC = TR.getCell((short)3);
						R = this.getRow((int)Loop);
						C = R.getCell((short)12);
						C.setCellStyle(TC.getCellStyle());
					
					
						this.set_CellFormula(13,Loop,"SUM(N" + StSum + ":N" + EnSum+ ")");
						TC = TR.getCell((short)3);
						R = this.getRow((int)Loop);
						C = R.getCell((short)13);					
						C.setCellStyle(TC.getCellStyle());
						
						if (TerTot_F.compareTo("Sum(M")==0)
						{
							TerTot_F = TerTot_F + (Loop+1);
							TerTot_U = TerTot_U + (Loop+1);							System.out.println("For_St:-" + TerTot_F);
						}
						else
						{
							TerTot_F = TerTot_F + ",M" + Loop+1;
							TerTot_U = TerTot_U + ",N" + Loop+1;
						}
						
						Loop=Loop+1;
						}
						
						CType = Rs.getString("type");
						if (CType.compareTo("P")==0) CntType = "Purchase Contracts";
						if (CType.compareTo("S")==0) CntType = "Sales Contracts";
						ChgTMth="N";
					}
					else
					{
						CType = Rs.getString("type");
						if (CType.compareTo("P")==0) CntType = "Purchase Contracts";
						if (CType.compareTo("S")==0) CntType = "Sales Contracts";
						ChgTMth="N";
					}
					this.setCellValue(4,Loop,CntType);
					R=this.getRow((int)Loop);
					TC = TR.getCell((short)1);
					C = R.getCell((short)4);
					C.setCellStyle(TC.getCellStyle());
					
					Loop=Loop+1;
				  	StSum = Loop;
				}
				
				
    			this.setCellValue(6,Loop,Rs.getString("ref_number"));
    			//System.out.println(Rs.getString("ref_number"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)6);
    			C.setCellStyle(TC.getCellStyle());
    			
    			String Ref ="";
    			String Name ="";
    			if (Type.compareTo("S")==0)
    			{
    				Name=pricefixed.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id"));
    				if (Rs.getString("supplier_buyer_ref")==null)
    				{
    					Ref = "-";
    				}
    				else
    				{	
    					if (Rs.getString("supplier_buyer_ref").compareTo("")==0)
    					{
    						Ref = "-";
    					}
    					else
    					{
    						Ref = Rs.getString("supplier_buyer_ref");
    					}
    				}	
    			}
    			else
    			{
    				Name=pricefixed.getFieldValueForId("company_master","name",Rs.getInt("supplier_buyer_id"));
    				if (Rs.getString("supplier_buyer_ref")==null)
    				{
    					Ref = "-";
    				}
    				else
    				{
    					if (Rs.getString("supplier_Buyer_ref").compareTo("")==0)
    					{
    						Ref = "-";
    					}
    					else
    					{
    						Ref = Rs.getString("supplier_buyer_ref");
    					}
    				}	
    			}
				this.setCellValue(7,Loop,Ref);
				//System.out.println(Ref);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)7);
    			C.setCellStyle(TC.getCellStyle());
    			
				this.setCellValue(8,Loop,Name);
				//System.out.println(Name);
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)8);
    			C.setCellStyle(TC.getCellStyle());
    			
				this.setCellValue(9,Loop,pricefixed.getFieldValueForId("grade_master","name",Rs.getInt("grade_id")));
				//System.out.println(pricefixed.getFieldValueForId("grade_master","name",Rs.getInt("grade_id")));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)4);
				C = R.getCell((short)9);
    			C.setCellStyle(TC.getCellStyle());
    			
				this.setCellValue(10,Loop,Rs.getFloat("contract_price_foreign"));
				//System.out.println(Rs.getFloat("contract_price_foreign"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)10);
    			C.setCellStyle(TC.getCellStyle());
    			
				this.setCellValue(11,Loop,Rs.getFloat("differential"));
				//System.out.println(Rs.getFloat("differential"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)11);
    			C.setCellStyle(TC.getCellStyle());
    			
				this.setCellValue(12,Loop,Rs.getFloat("fixed_tons"));
				//System.out.println(Rs.getFloat("fixed_tons"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)12);
    			C.setCellStyle(TC.getCellStyle());
    			
				Fton=Fton+Rs.getFloat("fixed_tons");
				this.setCellValue(13,Loop,Rs.getFloat("unfixed_tons"));
				//System.out.println(Rs.getFloat("unfixed_tons"));
				R=this.getRow((int)Loop);
				TC = TR.getCell((short)5);
				C = R.getCell((short)13);
    			C.setCellStyle(TC.getCellStyle());
    			
				UFton = UFton+Rs.getFloat("unfixed_tons");
				Loop=Loop+1;
			}
			EnSum=Loop;
			
			this.setCellValue(4,Loop,"Total");
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)0);
			C = R.getCell((short)4);
			C.setCellStyle(TC.getCellStyle());
			EnSum = Loop;
  		
			this.set_CellFormula(12,Loop,"SUM(M" + StSum + ":M" + EnSum+ ")");
			TC = TR.getCell((short)3);
			R = this.getRow((int)Loop);
			C = R.getCell((short)12);
			C.setCellStyle(TC.getCellStyle());
		
		
			this.set_CellFormula(13,Loop,"SUM(N" + StSum + ":N" + EnSum+ ")");
			TC = TR.getCell((short)3);
			R = this.getRow((int)Loop);
			C = R.getCell((short)13);					
			C.setCellStyle(TC.getCellStyle());

			if (TerTot_F.compareTo("Sum(M")==0)
			{
				TerTot_F = TerTot_F + (Loop+1) + ")";
				TerTot_U = TerTot_U + (Loop+1) + ")";
			}
			else
			{
				TerTot_F = TerTot_F + ",M" + (Loop+1) + ")";
				TerTot_U = TerTot_U + ",N" + (Loop+1) + ")";							
			}	
			Loop=Loop+2;
			this.setCellValue(3,Loop,TerMth +" Total");
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)0);
			C = R.getCell((short)3);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(12,Loop,TerTot_F);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)3);
			C = R.getCell((short)12);
			C.setCellStyle(TC.getCellStyle());
			
			this.set_CellFormula(13,Loop,TerTot_U);
			R=this.getRow((int)Loop);
			TC = TR.getCell((short)3);
			C = R.getCell((short)13);
			C.setCellStyle(TC.getCellStyle());						
			Loop=Loop+2;
	  		TerTot_F = "Sum(M";	
	  		TerTot_U = "Sum(N";				
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
	  

}